package com.hualan.servlet;

import com.alibaba.fastjson.JSON;
import com.hualan.service.CommunityUpdateService;
import com.hualan.service.impl.CommunityUpdateServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/community/addCommunity")
public class CommunityAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try{
            Map<String,Object> map = new HashMap<>();
            //communityName, communityProvince, communityCity, communityDistrict, communityAreaCode, communityLatitude, communityLongitude, communityAddress, communityId, communityCoordinates
            String communityName = req.getParameter("communityName");
            String communityProvince = req.getParameter("communityProvince");
            System.out.println(communityProvince);
            String communityCity = req.getParameter("communityCity");
            String communityDistrict = req.getParameter("communityDistrict");
            String communityAreaCode = req.getParameter("communityAreaCode");
            if (communityAreaCode == null || "".equals(communityAreaCode)){
                communityAreaCode = "0";
            }
            int areaCode = Integer.parseInt(communityAreaCode);
            String communityLatitude = req.getParameter("communityLatitude");
            String communityLongitude = req.getParameter("communityLongitude");
            String communityAddress = req.getParameter("communityAddress");
            communityAddress = communityProvince + communityCity + communityDistrict + communityAddress;
            String communityCoordinates = req.getParameter("communityCoordinates");

            CommunityUpdateService communityUpdateService = new CommunityUpdateServiceImpl();
            int row = communityUpdateService.addCommunity(communityName, communityProvince, communityCity, communityDistrict, areaCode, communityLatitude, communityLongitude, communityAddress, communityCoordinates);
            if (row < 0){
                map.put("code",302);
                map.put("message","新增失败");
            }else {
                map.put("code",200);
                map.put("message","新增成功");
            }
            resp.getWriter().write(JSON.toJSONString(map));
        } catch (Exception e){
            e.printStackTrace();
        }

    }

}
